package com.aidong.web.controller;

/**
 * Created by john on 16-11-3.
 */

import com.aidong.cache.ItemCache;
import com.aidong.cache.NameItem;
import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.wills.SysUserLogDao;
import com.aidong.model.LoginUser;
import com.aidong.model.RequestInfo;
import com.aidong.model.wills.SysUserLog;
import com.aidong.service.SysUserSession;
import com.aidong.util.FuncUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;

public class BaseController {
    protected Logger log = Logger.getLogger(this.getClass());
    private static final String user_key = "wx^T-usL";
    @Resource
    protected SysUserSession userSession;
    @Resource
    protected SysUserLogDao sysUserLogDao;
    @Resource
    protected ItemCache cache;

    public BaseController() {
    }

    protected Long parseLong(String str) {
        if (str != null && str.length() > 0) {
            try {
                return Long.valueOf(Long.parseLong(str));
            } catch (Exception var3) {
                ;
            }
        }

        return null;
    }

    protected Integer parseInt(String str) {
        if (str != null && str.length() > 0) {
            try {
                return Integer.valueOf(Integer.parseInt(str));
            } catch (Exception var3) {
                ;
            }
        }

        return null;
    }

    protected Double parseDouble(String str) {
        if (str != null && str.length() > 0) {
            try {
                return Double.valueOf(Double.parseDouble(str));
            } catch (Exception var3) {
            }
        }

        return null;
    }

    protected Date parseDate(String str, String format) {
        return this.parseDate(str, new SimpleDateFormat(format));
    }

    protected Date parseDate(String str, SimpleDateFormat sdf) {
        if (str != null && str.length() > 0) {
            try {
                return sdf.parse(str);
            } catch (Exception var4) {
                ;
            }
        }

        return null;
    }

    protected boolean chkNull(String str) {
        return str == null || str.length() == 0;
    }

    protected boolean chkNull(Date dt) {
        return dt == null;
    }

    protected boolean chkNull(Number n) {
        return n == null;
    }

    protected boolean chkNullZ(Number n) {
        return n == null || n.doubleValue() <= 0.0D;
    }

    protected String nvl(String s) {
        return s == null ? "" : s;
    }

    protected String nvl(Object s) {
        return s == null ? "" : s.toString();
    }

    protected String nvl(String s1, String s2) {
        return s1 == null ? (s2 == null ? "" : s2) : s1;
    }

    protected String trim(String s) {
        return s == null ? "" : s.trim();
    }

    protected double roundMoney(double d) {
        return (double) Math.round(d);
    }

    protected boolean equals(double d1, double d2) {
        return Math.abs(d1 - d2) < 0.01D;
    }

    protected String getLeft(String str, int len) {
        return this.getLeft(str, len, false);
    }

    protected String getLeft(String str, int len, boolean ad) {
        int n = 0;
        if (str != null && len > 0 && len < str.length() * 2) {
            if (ad) {
                len -= 3;
            }

            int i;
            for (i = 0; i < str.length(); ++i) {
                ++n;
                if (str.charAt(i) > 255) {
                    ++n;
                }

                if (n >= len) {
                    break;
                }
            }

            return i < str.length() ? str.substring(0, i) + (ad ? "..." : "") : str;
        } else {
            return str;
        }
    }

    protected int getLen(String str) {
        if (str == null) {
            return 0;
        } else {
            int n = 0;

            for (int i = 0; i < str.length(); ++i) {
                ++n;
                if (str.charAt(i) > 255) {
                    ++n;
                }
            }

            return n;
        }
    }

    protected String convXmlStr(String str) {
        if (str == null) {
            return "";
        } else {
            str = str.replaceAll("<", "&lt;");
            str = str.replaceAll(">", "&gt;");
            str = str.replaceAll("&", "&amp;");
            str = str.replaceAll("\'", "&apos;");
            str = str.replaceAll("\"", "&quot;");
            return str;
        }
    }

    protected String convStr(String str) {
        return str == null ? "" : str.replaceAll("\"", "\\\\\"").replaceAll("\n", "\\\\n").replaceAll("\r", "");
    }

    protected String getContent(String str, String s1, String s2) {
        String s = null;
        int n = str.indexOf(s1);
        if (n >= 0) {
            str = str.substring(n + s1.length());
            n = str.indexOf(s2);
            if (n >= 0) {
                s = str.substring(0, n);
            }
        }

        return s;
    }

    protected String getFail(int code, String msg) {
        return "{\"ret\":false, \"code\":" + code + ", \"msg\":\"" + this.convStr(msg) + "\"}";
    }

    protected String getSuccess(String data, String msg) {
        if (data != null && !data.startsWith("{") && !data.startsWith("[") && !data.startsWith("\"")) {
            data = "\"" + this.convStr(data) + "\"";
        }

        return "{\"ret\":true, \"data\":" + data + ", \"msg\":\"" + this.convStr(msg) + "\"}";
    }

    protected String getResult(boolean ret, String data, String msg) {
        if (data != null && !data.startsWith("{") && !data.startsWith("[") && !data.startsWith("\"")) {
            data = "\"" + this.convStr(data) + "\"";
        }

        return "{\"ret\":" + (ret ? "true" : "false") + ", \"data\":" + data + ", \"msg\":\"" + this.convStr(msg) + "\"}";
    }

    protected String getResult(boolean ret, String msg) {
        return "{\"ret\":" + (ret ? "true" : "false") + ", \"msg\":\"" + this.convStr(msg) + "\"}";
    }

    protected String getSetting(String keyId) {
        return this.cache.getValue("setting", keyId);
    }

    protected List<NameItem> getTypeList(String type) {
        return this.cache.getItemList(type);
    }

    protected boolean reloadDictTypeList(String dictType) {
        NameItem item = this.cache.getNameItem("dictType", dictType);
        if (item != null) {
            if ("1".equals(dictType)) {
                this.reloadTypeList("dictList");
            }

            return this.reloadTypeList(item.getProp());
        } else {
            return false;
        }
    }

    protected boolean reloadTypeList(String type) {
        return this.cache.remove(type);
    }

    protected ModelAndView showAlert(String msg) {
        ModelAndView mv = new ModelAndView("sys/alert");
        mv.addObject("msg", msg);
        return mv;
    }

    protected Date getDateFrom(String dt) {
        return dt == null ? null : this.parseDate(dt + " " + this.getSetting("account.time"), "yyyy-MM-dd HH:mm:ss");
    }

    protected Date getDateTo(String dt) {
        if (dt == null) {
            return null;
        } else {
            Date date = this.parseDate(dt + " " + this.getSetting("account.time"), "yyyy-MM-dd HH:mm:ss");
            return date == null ? null : new Date(date.getTime() + 86400000L - 1L);
        }
    }

    protected Date getAccountDate(Date dt) {
        if (dt == null) {
            return null;
        } else {
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            if (cal.get(11) < 2) {
                cal.add(5, -1);
            }

            return this.getTrimDate(dt);
        }
    }

    protected Date getTrimDate(Date dt) {
        if (dt == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            return this.parseDate(sdf.format(dt), sdf);
        }
    }

    protected String getEncodePwd(String loginName, String password) {
        return FuncUtil.Md5(loginName + password + "wx^T-usL");
    }

    public String getLoginUserCode() {
        LoginUser user = this.userSession.getLoginUser();
        return user != null ? user.getUserCode() : null;
    }

    public String getLoginShopCode() {
        LoginUser user = this.userSession.getLoginUser();
        return user != null ? user.getShopCode() : null;
    }

    public boolean isShopTypeCenter() {
        LoginUser user = this.userSession.getLoginUser();
        return user != null && user.getLoginShop() != null ? "02".equals(user.getLoginShop().getShopType()) : false;
    }

    public boolean queryOwn() {
        boolean b = true;
        LoginUser user = this.userSession.getLoginUser();
        if (user != null && user.getUserInfo() != null && user.getUserInfo().getQueryOwnData() != null && user.getUserInfo().getQueryOwnData().intValue() == 0) {
            b = false;
        }

        return b;
    }

    public ConditionMap addConditionShopCode(ConditionMap cd, String name, String shopCode) {
        LoginUser user = this.userSession.getLoginUser();
        if (user != null && user.getLoginShop() != null) {
            if ("01".equals(user.getLoginShop().getShopType())) {
                return cd.eq(name, user.getLoginShop().getCode());
            } else if (this.chkNull(shopCode) && user.getUserInfo().getQueryAllShop().intValue() == 1) {
                return cd;
            } else {
                ArrayList shops = new ArrayList();
                if (this.chkNull(shopCode)) {
                    Iterator ss = user.getShopsMap().keySet().iterator();

                    while (ss.hasNext()) {
                        String arr$ = (String) ss.next();
                        shops.add(arr$);
                    }
                } else {
                    String[] var11 = shopCode.split(",");
                    String[] var12 = var11;
                    int len$ = var11.length;

                    for (int i$ = 0; i$ < len$; ++i$) {
                        String s = var12[i$];
                        if (user.getShopsMap().containsKey(s)) {
                            shops.add(s);
                        }
                    }
                }

                return shops.size() == 0 ? cd.eq(name, "none") : (shops.size() == 1 ? cd.eq(name, shops.get(0)) : cd.in(name, shops.toArray()));
            }
        } else {
            return cd.eq(name, "none");
        }
    }

    public boolean chkAuthority(String op) {
        return this.userSession.chkAuthority(op);
    }

    public String getIpAddr() {
        RequestInfo req = this.userSession.getRequestInfo();
        return req != null ? req.getIpAddr() : null;
    }

    public void addUserLog(String operate, String content) {
        try {
            LoginUser e = this.userSession.getLoginUser();
            if (e == null) {
                return;
            }

            RequestInfo req = this.userSession.getRequestInfo();
            SysUserLog userLog = new SysUserLog();
            userLog.setLogTime(new Date());
            userLog.setUserCode(e.getUserCode());
            userLog.setShopCode(e.getShopCode());
            userLog.setOperate(operate);
            userLog.setContent(content);
            if (req != null) {
                userLog.setIpAddr(req.getIpAddr());
            }

            this.sysUserLogDao.add(userLog);
        } catch (Exception var6) {
            this.log.error("addUserLog", var6);
        }

    }
}
